package jcore.jsonrpc.rpcobj;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ag.bean.TbLog;
import com.ag.bean.TbLogType;
import com.ag.lwc.service.impl.LogService;
import com.ag.zhh.service.ContactLogService;

import jcore.jsonrpc.common.JsonRpcObject;
/*
 * 我的通信平台
 * */
public class LwcCommunicateStates extends JsonRpcObject implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1243352566878046786L;
	//所有坐席状态Map
	public static Map<String,CallState> statesMap=new HashMap<String,CallState>();
	
	public LwcCommunicateStates() {
		
	}
	//根据坐席id获取坐席的通信信息
	public CallState getCallState(String key) {
		 Map<String,CallState> map = getStatesMap();
		 if(!map.containsKey(key)){
			 CallState state =getNewCallState();
		//该两行是测试用的,到时要去掉
			 state.agentId=key;
			 state.localDeviceId=key;
			 map.put(key,state );
			 System.out.println("getCallState---------if");
			 System.out.println(" 坐席 "+key+" 不在位,现在入座");
		 }
		 System.out.println("getCallState---------"+map.get(key).getServerIp());
		 return map.get(key);		 
	}
	
	//获取所有坐席状态的哈希集
	public static Map<String,CallState> getStatesMap() {
		if(statesMap == null){
			statesMap = new HashMap<String,CallState>();
		}
		return statesMap;
	}
	//新的坐席
	private CallState getNewCallState() {
		return new CallState();
	}
	
	private LwcCommunicateStates getCommunicateStatesObj() {
		return this;
	}
	
	//坐席要召开电话会议
	public void makeConferencd(String agentId)			
	{
		CallState agentCallState = getCallState(agentId);
		agentCallState.callFlag=1;								//表示是多方通话
		System.out.println("***********坐席要召开电话会议：agentId :"+agentId+"  目的设备："+agentCallState.called+"flag:"+agentCallState.callFlag+" **************************");
	}
	//坐席要建立双方通话
	public void makecall(String agentId)
	{
		CallState agentCallState = getCallState(agentId);
		agentCallState.callFlag=0;								//表示是多方通话
		System.out.println("***********坐席要进行双方通话：agentId :"+agentId+"  callFlag:"+agentCallState.callFlag+"**************************");
	}
	//建立了单方通话
	public void establishCall(int callid,String caller,String called,String agentId)
	{
		CallState agentCallState = getCallState(agentId);
		if(agentCallState!=null)
		{
			agentCallState.callId=callid;
			agentCallState.caller=caller;
			agentCallState.called=called;
			agentCallState.recordStartTime=new Date();
		}
		else{
			System.out.println();
		}
		System.out.println("**************************建立了双方通话：callid="+callid+" caller="+caller+" called="+called);
	}
	//表示某方加入了电话会议
	public void transferConference(String calling,String user,int otherCallid,String agentId)
	{
		CallState agentCallState = getCallState(agentId);
		agentCallState.callId=otherCallid;
		agentCallState.caller=calling;
		if(agentCallState.called==null) agentCallState.called=user;
		else agentCallState.called=agentCallState.called+","+user;
		System.out.println("**************************某方加入了电话会议：callid="+otherCallid+" caller="+calling+" user="+user);
	}
	//收取通话的录音文件,收取录音文件和录开始时间
	public void receiveFilename(String agentId,String fileName)
	{
		CallState agentCallState = getCallState(agentId);
		agentCallState.filename=fileName;
		agentCallState.recordStartTime = new Date();
		System.out.println("********************** 后台收到录音文件："+fileName+"通话开始时间："+agentCallState.recordStartTime+" ************************");
		
	}

	/**
	 * 结束通话:要保存通话日志和置通话标志为0
	 */
	public void endCall(String agentId)
	{
		CallState agentCallState = getCallState(agentId);
		
		agentCallState.recordEndTime = new Date();
		System.out.println("结束通话： callid:"+agentCallState.callId+"  caller: "+agentCallState.caller+"   called: "+agentCallState.called+"   flag:"+agentCallState.callFlag+"   filename: "+agentCallState.filename+"  starttime: "+agentCallState.recordStartTime+"   endtime: "+agentCallState.recordEndTime);
		ContactLogService service = getContactLogService();
		service.addPhoneContactLog(agentCallState.recordStartTime, agentCallState.recordEndTime, agentCallState.caller, agentCallState.called, agentCallState.filename);			//保存通信日志		
		
		LogService logService = this.getLogService();			//插入日志
		TbLog log = new TbLog();
		TbLogType logType = new TbLogType();
		logType.setLtId(LogService.LOG_CONTACT);
		log.setTbLogType(logType);
		log.setLogContent("主叫："+agentCallState.caller+"  被叫："+agentCallState.called+"  录音文件："+agentCallState.filename);
		log.setLogTime(new Date());
		logService.save(log);
		//清空坐席状态
		agentCallState.callFlag=0;
	//	agentCallState.agentId=null;
		agentCallState.caller=null;
		agentCallState.called=null;
		agentCallState.filename=null;
		agentCallState.recordStartTime=null;
		agentCallState.recordEndTime=null;
		
		
		
	}
	//定义坐席的通信状态的内部类
	public class CallState implements Serializable  {
		public static final long serialVersionUID = -6185866059267238276L;
		public String serverIp;
		public String localDeviceId="8000";				//本地设备号码		
		public int localDeviceType;		
		public String agentId;								
		public String agentPassword;		
		public String queue;
		public int callId;	
		public String caller;			//主叫
		public String called;				//被叫号码串		
		public String filename;			//录音文件名		
		public Date recordStartTime=null;		//录音开始时间		
		public Date recordEndTime = null;		//录音结束时间
		public int callFlag=0;					//当正在通话时，0表示两方通话，1表示多方会议
		
	/*	public String faxTaskId;			//传真任务id		
		public String faxCalled;			//传真的目的设备字符串	
		*/
		public Map<String,String> faxTaskList;			//传真任务列表
		public String msgId;				//短信发送的目标字符串					
		public String smsCalled;			//短信暂时不考虑		
		
		public CallState() {
			
		}
		public String getServerIp() {
			return serverIp;
		}
		public void setServerIp(String serverIp) {		
			this.serverIp = serverIp;
		}
		public String getLocalDeviceId() {
			return localDeviceId;
		}
		public void setLocalDeviceId(String localDeviceId) {
			this.localDeviceId = localDeviceId;
		}
		public int getLocalDeviceType() {
			return localDeviceType;
		}
		public void setLocalDeviceType(int localDeviceType) {
			this.localDeviceType = localDeviceType;
		}
		public String getAgentId() {
			return agentId;
		}
		public void setAgentId(String agentId) {
			this.agentId = agentId;
		}
		public String getAgentPassword() {
			return agentPassword;
		}
		public void setAgentPassword(String agentPassword) {
			this.agentPassword = agentPassword;
		}
		public String getQueue() {
			return queue;
		}
		public void setQueue(String queue) {
			this.queue = queue;
		}
		public int getCallId() {
			return callId;
		}
		public void setCallId(int callId) {
			this.callId = callId;
		}
		public String getCalled() {
			return called;
		}
		public void setCalled(String called) {
			this.called = called;
		}
		/*
		public String getFaxTaskId() {
			return faxTaskId;
		}
		public void setFaxTaskId(String faxTaskId) {
			this.faxTaskId = faxTaskId;
		}
		public String getFaxCalled() {
			return faxCalled;
		}
		public void setFaxCalled(String faxCalled) {
			this.faxCalled = faxCalled;
		}
		*/
		public String getMsgId() {
			return msgId;
		}
		public void setMsgId(String msgId) {
			this.msgId = msgId;
		}
		public String getSmsCalled() {
			return smsCalled;
		}
		public void setSmsCalled(String smsCalled) {
			this.smsCalled = smsCalled;
		}
		public String getCaller() {
			return caller;
		}
		public void setCaller(String caller) {
			this.caller = caller;
		}
		public String getFilename() {
			return filename;
		}
		public void setFilename(String filename) {
			this.filename = filename;
		}
		public Date getRecordStartTime() {
			return recordStartTime;
		}
		public void setRecordStartTime(Date recordStartTime) {
			this.recordStartTime = recordStartTime;
		}
		public Date getRecordEndTime() {
			return recordEndTime;
		}
		public void setRecordEndTime(Date recordEndTime) {
			this.recordEndTime = recordEndTime;
		}
		public int getCallFlag() {
			return callFlag;
		}
		public void setCallFlag(int callFlag) {
			this.callFlag = callFlag;
		}

	}
	
	
	private static CallState MapToCallState(CallState callState,Map<String,String> callStateMap) {
		
		callState.setAgentId(callStateMap.get("agentId"));
		callState.setAgentPassword(callStateMap.get("agentPassword"));
		callState.setCalled(callStateMap.get("called"));
		if(callStateMap.get("callId") != null)
			callState.setCallId(Integer.parseInt(callStateMap.get("callId")));
	//	callState.setFaxCalled(callStateMap.get("faxCalled"));
	//	callState.setFaxTaskId(callStateMap.get("faxTaskId"));
		callState.setLocalDeviceId(callStateMap.get("localDeviceId"));
		if(callStateMap.get("localDeviceType") != null)
			callState.setLocalDeviceType(Integer.parseInt(callStateMap.get("localDeviceType")));
		callState.setMsgId(callStateMap.get("msgId"));
		callState.setQueue(callStateMap.get("queue"));
		callState.setServerIp(callStateMap.get("serverIp"));
		callState.setSmsCalled(callStateMap.get("smaCalled"));
		
		return callState;
	}
/*
 * 设置坐席的通讯状态
 * */
	public void setCallState(String key,Map<String,String> callStateMap) {
	 Map<String,CallState> map = getStatesMap();
	 System.out.println("setCallState---------callStateMap"+callStateMap.get("serverIp"));
	 System.out.println(map.containsKey(key));
	 if(!map.containsKey(key)){
		 map.put(key,MapToCallState(getNewCallState(),callStateMap));
		 System.out.println("setCallState---------if");
	 }else{
		 try{
		 CallState callState = map.get(key);
		 callState = MapToCallState(callState,callStateMap);
		 System.out.println("setCallState---------callState"+callState.getServerIp());
		 map.put(key,callState);
		 }catch(Exception e){
			 e.printStackTrace();
		 }
	 }
	}
	private ContactLogService getContactLogService() {
		ServletContext sc = getRequest().getSession().getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		ContactLogService contactService = (ContactLogService) ac.getBean("ContactLogService");
		return contactService;
	}
	
	private LogService getLogService() {
		ServletContext sc = getRequest().getSession().getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		LogService logService = (LogService) ac.getBean("LogService");
		return logService;
	}
	
	//查询所有坐席状态，测试用
	public void queryAllAgentState()
	{
		//遍历Map
		for(Map.Entry<String, CallState> entry:statesMap.entrySet())
		{
			CallState state = entry.getValue();
			System.out.println("agentId: "+state.getAgentId()+" 本地设备号码："+state.localDeviceId+" caller: "+state.getCaller()+" called: "+state.getCalled()+" callid: "+state.getCallId()+" callFlag: "+state.getCallFlag()+" filename: "+state.getFilename()+" 通话开始时间："+state.getRecordStartTime()+" 通话结束时间 "+state.getRecordEndTime());
		}
	}
	
}
